#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent),
     ui(new Ui::MainWindow)
{
        ui->setupUi(this);
        car.CarBase = new StackCar;            //停车场栈初始化
        car.CarBase->top = 0;
        car.CarBase->CarStack[car.CarBase->top] = NULL;

        car.QuitTemp = new StackCar;         //车辆临时栈初始化
        car.QuitTemp->top = 0;
        car.QuitTemp->CarStack[car.QuitTemp->top] = NULL;

        car.WaitQueue = new LinkQueueCar;       //便道链队初始化
        car.WaitQueue->head = new QCarNode;
        if (car.WaitQueue->head != NULL)
        {
            car.WaitQueue->head->next = NULL;
            car.WaitQueue->rear = car.WaitQueue->head;
        }
         connect(ui->pushButton,SIGNAL(clicked(bool)),ui->pushButton_2,SIGNAL(clicked(bool)));
         connect(ui->pushButton,SIGNAL(clicked(bool)),ui->pushButton_3,SIGNAL(clicked(bool)));
}

MainWindow::~MainWindow()
{
        delete ui;
        QCarNode *ptemp;
        while (car.CarBase->top != 0)
            car.CarBase->CarStack[--car.CarBase->top] = NULL;
        while (car.WaitQueue->head != car.WaitQueue->rear)
        {
            ptemp = car.WaitQueue->head;
            car.WaitQueue->head = car.WaitQueue->head->next;
            delete ptemp;
        }
}


void MainWindow::on_pushButton_clicked()
{
    CarNode *p;       //汽车临时指针
       QCarNode *t;      //队列中汽车临时指针

       p = new CarNode;
      p->num=ui->lineEdit->text();
      ui->lineEdit->clear();

       if (car.CarBase->top < MAX)       //有空车位
       { p->start=time(NULL);
           car.CarBase->top++;


           car.CarBase->CarStack[car.CarBase->top] = p;

       }
       else           //没有空车位，存入便道
       {

           t = new QCarNode;
           t->data = p;
           t->next = NULL;
           car.WaitQueue->rear->next = t;
           car.WaitQueue->rear = t;

       }

}

void MainWindow::on_pushButton_2_clicked() //查询停车库停车情况
{
    int i;
        if (car.CarBase->top>0)
        {
         ui->listWidget->addItem("-------------");
          ui->listWidget->addItem("车库停车情况");
            for (i = 1; i <= car.CarBase->top; i++)
            {
               QString str="车位号："+QString::number(i)+"   车牌号："+car.CarBase->CarStack[i]->num;
               ui->listWidget->addItem(str);

            }

        }
        else
            ui->listWidget->addItem("车库无车");

}

void MainWindow::on_pushButton_3_clicked() //便道停车情况
{
    QCarNode *p;
       p = car.WaitQueue->head->next;

       if (car.WaitQueue->head != car.WaitQueue->rear)
       {
           ui->listWidget_2->addItem("-------------");
            ui->listWidget_2->addItem("便道停车情况");
           while (p != NULL)
           {

               QString str="车牌号："+p->data->num;
               ui->listWidget_2->addItem(str);
               p = p->next;
           }

       }
       else
       {

          ui->listWidget_2->addItem("便道中无车");

       }
}

void MainWindow::on_pushButton_4_clicked()
{
    int item;
        CarNode *p, *t;
        QCarNode *q;
        if (car.CarBase->top > 0)      //车站有车时
        {
            item=ui->lineEdit_2->text().toInt();
            while (car.CarBase->top > item)  //位置不在栈顶的汽车出站
            {
                car.QuitTemp->top++;
                car.QuitTemp->CarStack[car.QuitTemp->top] = car.CarBase->CarStack[car.CarBase->top];
                car.CarBase->CarStack[car.CarBase->top] = NULL;
                car.CarBase->top--;
            }

            p = car.CarBase->CarStack[car.CarBase->top];
            car.CarBase->CarStack[car.CarBase->top] = NULL;
            car.CarBase->top--;

            while (car.QuitTemp->top >= 1)  //当暂时存储汽车的栈结构中有汽车时汽车重新进站
            {
                car.CarBase->top++;
                car.CarBase->CarStack[car.CarBase->top] = car.QuitTemp->CarStack[car.QuitTemp->top];
                car.QuitTemp->CarStack[car.QuitTemp->top] = NULL;
                car.QuitTemp->top--;
            }

               p->stop=time(NULL);
            QString str="离开车辆的车牌号为:"+p->num+"\n应交费用为: "+QString::number((p->stop-p->start)*0.05)+"元";

ui->listWidget_3->addItem(str);

                free(p);

            if ((car.WaitQueue->head != car.WaitQueue->rear) && car.CarBase->top<MAX)    //停车场有车位且便道有车时，便道中车辆进入停车场
            {

                q = car.WaitQueue->head->next;
                t = q->data;
                car.CarBase->top++;



                car.WaitQueue->head->next = q->next;
                if (q == car.WaitQueue->rear)
                    car.WaitQueue->rear = car.WaitQueue->head;
                car.CarBase->CarStack[car.CarBase->top] = t;
                car.CarBase->CarStack[car.CarBase->top]->start=time(NULL);
                free(q);
            }

        }

}

void MainWindow::on_pushButton_5_clicked()
{
    QCarNode *p,*q;
        p = car.WaitQueue->head->next;
        q = car.WaitQueue->head;
        if (car.WaitQueue->head != car.WaitQueue->rear)
        {
            while (p!= NULL)
            {if(p->data->num==ui->lineEdit_3->text())  break;
                q = p;
                p = p->next;
            }
            if(p->data->num==ui->lineEdit_3->text())
            {ui->lineEdit_3->clear();
                q->next = p->next;
            ui->listWidget_4->addItem("便道离开，无需缴费");
            free(p);}
            else {
                 ui->listWidget_4->addItem("便道无此车");
            }
        }
        else
        {
             ui->listWidget_4->addItem("便道无车");
        }
}

void MainWindow::on_pushButton_7_clicked()
{
    ui->listWidget_2->clear();
}

void MainWindow::on_pushButton_6_clicked()
{
    ui->listWidget->clear();
}

void MainWindow::on_pushButton_8_clicked()
{
    close();
}
